﻿using System.Reflection;
using NLite.Internal;
using System;

namespace NLite.Reflection
{
    delegate object ConstructorHandler(params object[] arguments);

    /// <summary>
    /// 
    /// </summary>
    public static class CounstructorExtensions
    {
       /// <summary>
       /// 
       /// </summary>
       /// <typeparam name="T"></typeparam>
       /// <param name="ctor"></param>
       /// <returns></returns>
        public static Func<object[], T> GetCreator<T>(this ConstructorInfo ctor)
        {
            var handler = DynamicMethodFactoryProxy.Instance.CreateConstructorMethod(ctor);
            return args =>
            {
                if (args == null)
                    return (T)handler();
                return (T)handler(args);
                //return (T)ctor.FastInvoke(args);
            };
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="constructor"></param>
        /// <param name="args"></param>
        /// <returns></returns>
        public static object FastInvoke(this ConstructorInfo constructor, params object[] args)
        {
            if (args == null) args = new object[0];

            return DynamicMethodFactoryProxy.Instance.CreateConstructorMethod(constructor)(args);
        }
    }

}
